package org.ttitfly.ttcore.util.dozer;

import java.util.ArrayList;
import java.util.List;

import net.sf.dozer.util.mapping.MapperIF;

public class DozerHelper {

    @SuppressWarnings("unchecked")
    public static <P> P clone(P base) {

        if (base == null) {
            return null;
        } else {
            MapperIF mapper = DozerBeanMapperFactory.getMapper();
            return (P) mapper.map(base, base.getClass());
        }

    }

    public static <P> List<P> cloneList(List<P> baseList) {

        if (baseList == null) {
            return null;
        } else {
            List<P> targetList = new ArrayList<P>();
            for (P p : baseList) {

                targetList.add((P) clone(p));
            }
            return targetList;
        }

    }

    @SuppressWarnings("unchecked")
    public static <V, P> P convert(V base, Class<P> target) {

        if (base == null) {
            return null;
        } else {
            MapperIF mapper = DozerBeanMapperFactory.getMapper();
            return (P) mapper.map(base, target);
        }

    }

    @SuppressWarnings("unchecked")
    public static <V, P> P convert(V base, P target) {

        if (base != null) {
            MapperIF mapper = DozerBeanMapperFactory.getMapper();
            mapper.map(base, target);
            return target;
        }
        return target;
    }

    public static <V, P> List<P> convertList(List<V> baseList, Class<P> target) {

        if (baseList == null) {
            return null;
        } else {
            List<P> targetList = new ArrayList<P>();
            for (V vo : baseList) {

                targetList.add((P) convert(vo, target));
            }
            return targetList;
        }

    }

}
